<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-07-01 11:15
 *
 * Version: 2.0
 */

namespace App\Models;


use Encore\Admin\Traits\AdminBuilder;
use Encore\Admin\Traits\ModelTree;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
class Role extends Model
{
	use AdminBuilder, ModelTree {
		ModelTree::boot as treeBoot;
	}

	protected $hidden = ['created_at','updated_at'];
	protected $guarded = [];


	/**
	 * Create a new Eloquent model instance.
	 *
	 * @param array $attributes
	 */
	public function __construct(array $attributes = [])
	{
		$this->setTable('role');
		parent::__construct($attributes);
		$this->setTitleColumn('name');
	}


	/**
	 * A role belongs to many permissions.
	 *
	 * @return BelongsToMany
	 */
	public function authRule() : BelongsToMany
	{
		return $this->belongsToMany(AuthRule::class, 'role_auth_rule', 'role_id', 'auth_rule_id');
	}


	public function module(){
		return $this->belongsToMany(Module::class,'role_module','role_id','module_id');
	}


	protected static function boot()
	{
		parent::boot();

		static::deleting(function ($model) {
			$model->authRule()->detach();
		});
	}

}